class WordDictionary {

    private Map<Integer,Set<String>>map;

    public WordDictionary() {
        map=new HashMap<>();
    }
    
    public void addWord(String word) {
        int length=word.length();//按长度先分类减少搜索时间
        if(map.get(length)!=null){
            map.get(length).add(word);
        }else{
            Set<String>set=new HashSet<>();
            set.add(word);
            map.put(length,set);
        }
    }
    
    public boolean search(String word) {
        Set<String>set=map.get(word.length());
        if(set==null)return false;
        if(set.contains(word))return true;
        char[] chars=word.toCharArray();
        P:for(String s:set){
            if(word.length()!=s.length())continue;
            char[] cs=s.toCharArray();
            for(int i=0;i<cs.length;i++){
                if(chars[i]!='.'&&chars[i]!=cs[i])continue P;
            }
            set.add(word);
            return true;
        }
        return false;
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */